package com.yuelan.codelib.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Vibrator;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.yuelan.codelib.utils.encryption.MD5Encoder;

/**
 * 作者: peijiangping<BR>
 * 2012-12-17下午2:50:50<BR>
 * PhoneUtil.java<BR>
 */
public class PhoneUtil {
	/**
	 * 作者: peijiangping<BR>
	 * 时间:2012-12-17下午2:51:12<BR>
	 * 功能:打电话<BR>
	 * 返回值:void<BR>
	 */
	public static void call(String phoneNum, Context c) {
		Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
				+ phoneNum));
		c.startActivity(intent);
	}

	/**
	 * 作者: peijiangping<BR>
	 * 时间:2012-12-17下午2:49:39<BR>
	 * 功能:发送短信<BR>
	 * 返回值:void<BR>
	 */
	public static void sendMessage(String phoneNum, String content, Context c) {
		Uri smsToUri = Uri.parse("smsto:" + phoneNum);
		Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO,
				smsToUri);
		mIntent.putExtra("sms_body", content);
		c.startActivity(mIntent);
	}

	public static String getICCID(Context c) {
		TelephonyManager tm = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getSimSerialNumber(); // 取出MSISDN，很可能为空
	}

	/**
	 * 作者: peijiangping<BR>
	 * 时间:2012-12-17下午2:49:49<BR>
	 * 功能:获取电话号码<BR>
	 * 返回值:String<BR>
	 */
	public static String getNativePhoneNumber(Context c) {
		TelephonyManager telephonyManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		String phoneNum = telephonyManager.getLine1Number();
		return get11PhoneNum(phoneNum);
	}

	public static String getNativeOperatorName(Context c) {
		TelephonyManager telephonyManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getSimOperatorName();
	}

	// 获取手机型号
	public static String getDeviceType() {
		String model = android.os.Build.MODEL;
		model = model.replace(" ", "");
		model = model.replace("　", "");
		return model;
	}

	// 获取手机品牌
	public static final String getMobileManufacturer() {
		String manufacturer = "";
		Process pro = null;
		try {
			pro = Runtime.getRuntime().exec(
					"getprop apps.customerservice.device");
			BufferedReader br = new BufferedReader(new InputStreamReader(
					pro.getInputStream()));
			manufacturer = (br.readLine());
			manufacturer = manufacturer.toUpperCase();
			pro.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (pro != null)
				pro.destroy();
		}
		if (TextUtils.isEmpty(manufacturer))
			manufacturer = android.os.Build.MANUFACTURER;
		manufacturer = manufacturer.replace(" ", "");
		manufacturer = manufacturer.replace("　", "");
		return manufacturer.toUpperCase();
	}

	/**
	 * 作者: peijiangping<BR>
	 * 时间:2012-12-17下午2:55:31<BR>
	 * 功能:获取运营商信息<BR>
	 * 返回值:int<BR>
	 */

	public static String getMacAddress(Context c) {
		WifiManager wm = (WifiManager) c.getSystemService(Context.WIFI_SERVICE);
		return wm.getConnectionInfo().getMacAddress();
	}

	public static String getOnlyPhoneId(Context c) {
		String id = getDeviceId(c) + getIMEI(c);
		return MD5Encoder.EncoderByMd5(id);
	}

	public static String getPhoneID(Context c) {
		String imei = getIMEI(c);
		if (TextUtil.notNull(imei)) {
			return MD5Encoder.EncoderByMd5(imei);
		} else {
			return MD5Encoder.EncoderByMd5(getDeviceId(c));
		}
	}

	public static String isSimExist(Context c) {
		String state = null;
		TelephonyManager mTelephonyManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		int simState = mTelephonyManager.getSimState();
		switch (simState) {
		case TelephonyManager.SIM_STATE_ABSENT:
			state = "无卡";
			break;
		case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
			state = "需要NetworkPIN解锁";
			break;
		case TelephonyManager.SIM_STATE_PIN_REQUIRED:
			state = "需要PIN解锁";
			// do something
			break;
		case TelephonyManager.SIM_STATE_PUK_REQUIRED:
			state = "需要PUN解锁";
			// do something
			break;
		case TelephonyManager.SIM_STATE_READY:
			state = "良好";
			// do something
			break;
		case TelephonyManager.SIM_STATE_UNKNOWN:
			state = "未知状态";
			break;
		}
		return state;
	}

	public static String getAndroidVersion() {
		// // LogUtil.v("-----------");
		// // LogUtil.v(android.os.Build.DISPLAY);
		// // LogUtil.v(android.os.Build.VERSION.INCREMENTAL);
		// // LogUtil.v("-----------");
		return android.os.Build.VERSION.RELEASE;
	}

	public static String getIMSI(Context c) {
		TelephonyManager telephonyManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getSubscriberId();
	}

	public static String getIMEI(Context c) {
		TelephonyManager telephonyManager = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();
	}

	public static String getDeviceId(Context c) {
		return Secure.getString(c.getContentResolver(), Secure.ANDROID_ID);
	}

	public static String getPsdnIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()
							&& inetAddress instanceof Inet4Address) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
		}
		return "";
	}

	public static int validateMobile(String mobile) {
		if (mobile == null) {
			return 0;
		}
		mobile = get11PhoneNum(mobile);
		if (mobile.trim().length() != 11) {
			return 0;
		}
		if (mobile.trim().substring(0, 3).equals("134")
				|| mobile.trim().substring(0, 3).equals("135")
				|| mobile.trim().substring(0, 3).equals("136")
				|| mobile.trim().substring(0, 3).equals("137")
				|| mobile.trim().substring(0, 3).equals("138")
				|| mobile.trim().substring(0, 3).equals("139")
				|| mobile.trim().substring(0, 3).equals("182")
				|| mobile.trim().substring(0, 3).equals("150")
				|| mobile.trim().substring(0, 3).equals("151")
				|| mobile.trim().substring(0, 3).equals("152")
				|| mobile.trim().substring(0, 3).equals("157")
				|| mobile.trim().substring(0, 3).equals("158")
				|| mobile.trim().substring(0, 3).equals("159")
				|| mobile.trim().substring(0, 3).equals("187")
				|| mobile.trim().substring(0, 3).equals("188")) {
			return 1;
		} else if (mobile.trim().substring(0, 3).equals("130")
				|| mobile.trim().substring(0, 3).equals("131")
				|| mobile.trim().substring(0, 3).equals("132")
				|| mobile.trim().substring(0, 3).equals("156")
				|| mobile.trim().substring(0, 3).equals("185")
				|| mobile.trim().substring(0, 3).equals("186")) {
			return 2;
		} else if (mobile.trim().substring(0, 3).equals("133")
				|| mobile.trim().substring(0, 3).equals("153")
				|| mobile.trim().substring(0, 3).equals("180")
				|| mobile.trim().substring(0, 3).equals("189")) {
			return 3;
		}
		return 0;
	}

	private static String get11PhoneNum(String phone) {
		if (phone != null && phone.length() > 11) {
			phone = phone.substring(phone.length() - 11);
		}
		return phone;
	}

	public static void sendMessageOfSystem(Context c, String phonenumber) {
		Uri uri = Uri.parse("smsto:" + phonenumber);
		Intent it = new Intent(Intent.ACTION_SENDTO, uri);
		it.putExtra("sms_body", "");
		c.startActivity(it);
	}

	// 调用手机通知铃声
	public static void setNotificationRingtone(Context c) {
		Uri alert = RingtoneManager
				.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
		MediaPlayer mMediaPlayer = new MediaPlayer();
		try {
			mMediaPlayer.setDataSource(c, alert);
			mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 调用手机震动
	public static void setPhoneVibration(Context c, long milliseconds) {
		Vibrator vibrator = (Vibrator) c
				.getSystemService(Context.VIBRATOR_SERVICE);
		vibrator.vibrate(milliseconds);
	}
}
